package com.lanou.pianke.admin.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.pianke.articlecomment.entity.ArticleReply;
import com.lanou.pianke.articlecomment.entity.ArticleComment;
import com.lanou.pianke.admin.service.AreplyService;
import com.lanou.pianke.admin.service.ArticleCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class ArticleCommentController {

    @Autowired
    private ArticleCommentService articleCommentService;

    @Autowired
    private AreplyService areplyService;

    @GetMapping("/login")
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    //调用文章评论项目查看所有举报文章评论
    @GetMapping("/showAComments")
    public PageInfo<ArticleComment> showArticleComments(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "7") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<ArticleComment> articleComments = articleCommentService.showAll();
        for (ArticleComment articleComment : articleComments) {
            String acomid = articleComment.getAcomid();
            List<ArticleReply> articleReplies = areplyService.selectById(acomid);
            articleComment.setAreplyList(articleReplies);
        }
        PageInfo<ArticleComment> pageInfo = new PageInfo<>(articleComments);
        System.out.println(pageInfo.getPages());
        return pageInfo;
    }

    //删除举报文章评论
    @DeleteMapping("/deleteArticleComment/{id}")
    public String deleteComment(@PathVariable String id){
        articleCommentService.deleteById(id);
        List<ArticleReply> areplyList = areplyService.selectById(id);
        if (areplyList != null){
            areplyService.deleteById(id);
        }
        return "delete successfully";
    }

}
